/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hertzbeat.controller;

import org.apache.hertzbeat.common.entity.job.Configmap;
import org.apache.hertzbeat.common.entity.job.Job;
import org.apache.hertzbeat.common.entity.message.ClusterMsg;
import org.apache.hertzbeat.common.util.JsonUtil;
import org.apache.hertzbeat.scheduler.netty.ManageServer;
import org.apache.hertzbeat.service.AppService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/job")
public class SendJobController {

    @Autowired(required = false)
    private ManageServer manageServer;

    @Autowired(required = false)
    private AppService appService;

    @GetMapping(path = "/send/{isCyclic}")
    public boolean send(@PathVariable("isCyclic") Integer isCyclic) {
        Job appDefine = appService.getAppDefine("windows");
        appDefine.setId(DateUtil.DAY_MILLISECONDS);
        appDefine.setMonitorId(424116993974784L);
        appDefine.setCyclic(isCyclic == 1);
        if (isCyclic == 1) {
            appDefine.setInterval(10);
        }
        appDefine.setTimestamp(System.currentTimeMillis());
        List<Configmap> configmaps = new ArrayList<>();

        Configmap configmap = new Configmap();
        configmap.setKey("host");
        configmap.setValue("192.168.212.22");
        configmap.setType((byte) 1);
        configmaps.add(configmap);

        configmap = new Configmap();
        configmap.setKey("timeout");
        configmap.setValue("6000");
        configmap.setType((byte) 0);
        configmaps.add(configmap);

        configmap = new Configmap();
        configmap.setKey("community");
        configmap.setValue("suninfo");
        configmap.setType((byte) 1);
        configmaps.add(configmap);

        configmap = new Configmap();
        configmap.setKey("snmpVersion");
        configmap.setValue("1");
        configmap.setType((byte) 1);
        configmaps.add(configmap);

        configmap = new Configmap();
        configmap.setKey("port");
        configmap.setValue("161");
        configmap.setType((byte) 0);
        configmaps.add(configmap);

        appDefine.setConfigmap(configmaps);

        ClusterMsg.Message message = ClusterMsg.Message.newBuilder()
                .setType(isCyclic == 1 ? ClusterMsg.MessageType.ISSUE_CYCLIC_TASK : ClusterMsg.MessageType.ISSUE_ONE_TIME_TASK)
                .setDirection(ClusterMsg.Direction.REQUEST)
                .setMsg(JsonUtil.toJson(appDefine))
                .build();
        return manageServer.sendMsg("hertzbeat-collector-test", message);
    }
}
